package han.parser;

import han.entity.FriendEntity;
import han.entity.SelfInfoEntity;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Han
 * @date: 2018/7/11 14:10
 * @dscription: 好友信息解析器
 */
public class FriendParser {
    /**
     * 解析好友信息
     *
     * @param rowJsonStr json字符串
     * @return 好友list
     */
    public static List<FriendEntity> getFriendList(String rowJsonStr) {

        List<FriendEntity> entityList = new ArrayList<>();

        JSONObject rowJsonObj = new JSONObject(rowJsonStr);
        JSONObject resultJsonObj = rowJsonObj.getJSONObject("result");

        //解析分组信息
        JSONArray categoriesArray = resultJsonObj.getJSONArray("categories");
        Map<Integer, String> categoriesMap = new HashMap<>(10);
        categoriesMap.put(-1, "未知分组");
        categoriesMap.put(0, "我的好友");
        for (int categoriesIndex = 0; categoriesIndex < categoriesArray.length(); categoriesIndex++) {
            JSONObject tempJsonObj = categoriesArray.getJSONObject(categoriesIndex);
            int index = tempJsonObj.getInt("index");
            String name = tempJsonObj.getString("name");
            categoriesMap.put(index, name);
        }

        JSONArray friendsArray = resultJsonObj.getJSONArray("friends");
        JSONArray marknameArray = resultJsonObj.getJSONArray("marknames");
        JSONArray infoArray = resultJsonObj.getJSONArray("info");
        for (int infoIndex = 0; infoIndex < infoArray.length(); infoIndex++) {
            JSONObject tempJsonObj = infoArray.getJSONObject(infoIndex);
            long face = tempJsonObj.getLong("face");
            long flag = tempJsonObj.getLong("flag");
            long uin = tempJsonObj.getLong("uin");
            String nickName = tempJsonObj.getString("nick");
            FriendEntity entity = new FriendEntity();
            entity.setUin(uin);
            entity.setFace(face);
            entity.setNickName(nickName);
            entity.setFlag(flag);
            //获取分组编号
            for (int friendsIndex = 0; friendsIndex < friendsArray.length(); friendsIndex++) {
                JSONObject friendsJsonObj = friendsArray.getJSONObject(friendsIndex);
                long tempUin = tempJsonObj.getLong("uin");
                if (uin == tempUin) {
                    //如果uin相同,获取并设置分组编号,然后移除在该组中的元素,最后跳出
                    int categoriesIndex = friendsJsonObj.getInt("categories");
                    friendsArray.remove(friendsIndex);
                    entity.setCategoriesIndex(categoriesIndex);
                    break;
                } else {
                    entity.setCategoriesIndex(-1);
                }
            }
            //设置分组名称
            entity.setCategoriesName(categoriesMap.get(entity.getCategoriesIndex()));
            //获取昵称
            for (int marknameIndex = 0; marknameIndex < marknameArray.length(); marknameIndex++) {
                JSONObject marknameJsonObj = marknameArray.getJSONObject(marknameIndex);
                long tempUin = marknameJsonObj.getLong("uin");
                if (uin == tempUin) {
                    //如果uin相同,获取并设置昵称,然后移除在该组中的元素,最后跳出
                    String markname = marknameJsonObj.getString("markname");
                    marknameArray.remove(marknameIndex);
                    entity.setMarkName(markname);
                    break;
                } else {
                    entity.setMarkName(null);
                }
            }

            entityList.add(entity);
        }

        return entityList;
    }

    /**
     * 设置自己的信息
     *
     * @param rowStr 原始字符串
     */
    public static void selfInfo(String rowStr) {
        JSONObject rowJsonObj = new JSONObject(rowStr);
        JSONObject resultJsonObj = rowJsonObj.getJSONObject("result");
        JSONObject birthdayJsonObj = resultJsonObj.getJSONObject("birthday");
        String birthday = birthdayJsonObj.getLong("year") + "-" + birthdayJsonObj.getLong("month") + "-" +
                birthdayJsonObj.getLong("day");
        long face = resultJsonObj.getLong("face");
        String phone = resultJsonObj.getString("phone");
        String occupation = resultJsonObj.getString("occupation");
        long allow = resultJsonObj.getLong("allow");
        String college = resultJsonObj.getString("college");
        long uin = resultJsonObj.getLong("uin");
        long blood = resultJsonObj.getLong("blood");
        long constel = resultJsonObj.getLong("constel");
        String lnick = resultJsonObj.getString("lnick");
        String vfwebqq = resultJsonObj.getString("vfwebqq");
        String homepage = resultJsonObj.getString("homepage");
        long vipInfo = resultJsonObj.getLong("vip_info");
        String city = resultJsonObj.getString("city");
        String country = resultJsonObj.getString("country");
        String personal = resultJsonObj.getString("personal");
        long shengxiao = resultJsonObj.getLong("shengxiao");
        String nick = resultJsonObj.getString("nick");
        String email = resultJsonObj.getString("email");
        String province = resultJsonObj.getString("province");
        long account = resultJsonObj.getLong("account");
        String gender = resultJsonObj.getString("gender");
        String mobile = resultJsonObj.getString("mobile");
        SelfInfoEntity selfInfoEntity = SelfInfoEntity.getInstance();
        selfInfoEntity.setAccount(account);
        selfInfoEntity.setAllow(allow);
        selfInfoEntity.setBirthday(birthday);
        selfInfoEntity.setBlood(blood);
        selfInfoEntity.setCity(city);
        selfInfoEntity.setCollege(college);
        selfInfoEntity.setConstel(constel);
        selfInfoEntity.setCountry(country);
        selfInfoEntity.setEmail(email);
        selfInfoEntity.setFace(face);
        selfInfoEntity.setGender(gender);
        selfInfoEntity.setHomepage(homepage);
        selfInfoEntity.setLnick(lnick);
        selfInfoEntity.setMobile(mobile);
        selfInfoEntity.setNick(nick);
        selfInfoEntity.setOccupation(occupation);
        selfInfoEntity.setPersonal(personal);
        selfInfoEntity.setPhone(phone);
        selfInfoEntity.setProvince(province);
        selfInfoEntity.setShengxiao(shengxiao);
        selfInfoEntity.setUin(uin);
        selfInfoEntity.setVfwebqq(vfwebqq);
        selfInfoEntity.setVipInfo(vipInfo);
    }
}
